class Admin::CoursesController < ApplicationController
  
  uses_tiny_mce(:options => { :theme => "advanced",
                              :width => "100%",
                              :plugins => %w{advimage preview},
                              :theme_advanced_buttons1 => "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,image,insertfile,insertimage,cleanup,|,code,preview",
                              :theme_advanced_buttons2 => "",
                              :theme_advanced_toolbar_location => "top",
                              :theme_advanced_toolbar_align => "left",
                              :theme_advanced_statusbar_location => "bottom"
                            })
  def index
   @courses = Course.all
  end
  
  def show
    if !params[:id].nil?
      @course = Course.find(params[:id])
    else
      @falsh[:error] = "No such course to edit"
    end
  end
  
  def new
    @course = Course.new
  end
  
  def create
    if !params[:course].nil?
      @course = Course.new(params[:course])
      if @course.save 
        flash[:notice] = "New course #{@course.name} has been created"
        redirect_to :action=>'show' , :id=>@course.id
      else
        flash[:error] = "Failed to create #{@course.name}"
        render :action => "new"
      end
    else
      flash[:error] = "Failed to create a new course"
      render :action => "new"
    end
  end
  
  def edit
    if !params[:id].nil?
      @course = Course.find(params[:id])
    else
      flash[:error] = "No such course foound"
    end
  end
  
  def update
    if !params[:id].nil?
      @course = Course.find(params[:id])
      if @course.update_attributes(params[:course])
        flash[:notice] = "Course #{@course.name} has been updated"
      else
        flash[:error] = "Failed to update course #{@course.name}"
      end
    end
    redirect_to :action=>'show', :id=>@course.id
  end

end
